<template>
	<div>错误页面 {{ mapValue }}</div>
</template>

<script name="Exception">
import { useGo, useRedo } from '@/hooks/web/usePage';
import { PageEnum } from '@/enums/pageEnum';
import { ExceptionEnum } from '/@/enums/exceptionEnum';

export default defineComponent({
	name: 'ErrorPage',
	props: {
		// 状态码
		status: {
			type: Number,
			default: ExceptionEnum.PAGE_NOT_FOUND,
		},

		title: {
			type: String,
			default: '',
		},

		subTitle: {
			type: String,
			default: '',
		},

		full: {
			type: Boolean,
			default: false,
		},
	},
	setup(props) {
		const statusMapRef = ref(new Map());

		const { query } = useRoute();
		const go = useGo();
		const redo = useRedo();

		const getStatus = computed(() => {
			const { status: routeStatus } = query;
			const { status } = props;
			return Number(routeStatus) || status;
		});

		const getMapValue = computed(() => {
			return unref(statusMapRef).get(unref(getStatus));
		});

		const backLogin = '重新登录';
		const backHome = '返回首页';

		unref(statusMapRef).set(ExceptionEnum.PAGE_NOT_ACCESS, {
			title: '403',
			status: `${ExceptionEnum.PAGE_NOT_ACCESS}`,
			subTitle: '对不起，您无权访问此页面。',
			btnText: props.full ? backLogin : backHome,
			handler: () => (props.full ? go(PageEnum.BASE_LOGIN) : go()),
		});

		unref(statusMapRef).set(ExceptionEnum.PAGE_NOT_FOUND, {
			title: '404',
			status: `${ExceptionEnum.PAGE_NOT_FOUND}`,
			subTitle: '对不起，您访问的页面不存在。',
			btnText: props.full ? backLogin : backHome,
			handler: () => (props.full ? go(PageEnum.BASE_LOGIN) : go()),
		});

		unref(statusMapRef).set(ExceptionEnum.ERROR, {
			title: '500',
			status: `${ExceptionEnum.ERROR}`,
			subTitle: '对不起，服务器报告错误。',
			btnText: backHome,
			handler: () => go(),
		});

		unref(statusMapRef).set(ExceptionEnum.PAGE_NOT_DATA, {
			title: '当前页面上没有数据。',
			subTitle: '',
			btnText: '刷新',
			handler: () => redo(),
			// icon: notDataSvg,
		});

		unref(statusMapRef).set(ExceptionEnum.NET_WORK_ERROR, {
			title: '网络错误',
			subTitle: '对不起，您的网络连接已断开，请检查您的网络!',
			btnText: '刷新',
			handler: () => redo(),
			// icon: netWorkSvg,
		});
		const mapValue = unref(getMapValue) || {};

		return { mapValue };
	},
});
</script>
